package co.yixiang.controller.manager.adopt;

import co.yixiang.modules.adopt.entity.domain.YxAdoptContract;
import co.yixiang.modules.adopt.entity.dto.YxAdoptContractQueryCriteria;
import co.yixiang.modules.adopt.service.YxAdoptContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author hupeng
 * @date 2019-10-07
 */
@Api(tags = "认养:认养合同管理")
@RestController
@RequestMapping("api/yxAdoptContract")
public class AdoptContractController {
    @Autowired
    private YxAdoptContractService adoptContractService;

    @ApiOperation(value = "分页查询")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','YXADOPTCONTRACT_ALL','YXADOPTCONTRACT_SELECT')")
    public ResponseEntity getYxContractPage(YxAdoptContractQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(adoptContractService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation(value = "查询")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAnyRole('admin','common','YXADOPTCONTRACT_ALL','YXADOPTCONTRACT_SELECT')")
    public ResponseEntity getYxContractList(YxAdoptContractQueryCriteria criteria) {
        return new ResponseEntity(adoptContractService.queryAll(criteria), HttpStatus.OK);
    }

    @ApiOperation(value = "阅读")
    @GetMapping(value = "/read")
    public ResponseEntity readYxContract(YxAdoptContract criteria) {
        return new ResponseEntity(adoptContractService.findById(criteria), HttpStatus.OK);
    }


    @ApiOperation(value = "新增")
    @PostMapping
    @PreAuthorize("hasAnyRole('admin','common','YXADOPTCONTRACT_ALL','YXADOPTCONTRACT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YxAdoptContract resources) {
        return new ResponseEntity(adoptContractService.create(resources), HttpStatus.CREATED);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    @PreAuthorize("hasAnyRole('admin','common','YXADOPTCONTRACT_ALL','YXADOPTCONTRACT_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YxAdoptContract resources) {
        adoptContractService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyRole('admin','common','YXADOPTCONTRACT_ALL','YXADOPTCONTRACT_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        adoptContractService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

//    @ApiOperation(value = "发布文章")
//    @GetMapping(value = "/yxArticle/publish/{id}")
//    @PreAuthorize("hasAnyRole('admin','common','YXARTICLE_ALL','YXARTICLE_DELETE')")
//    public ResponseEntity publish(@PathVariable Integer id) throws Exception {
//
//        YxArticleDto yxArticleDTO = new YxArticleDto();
//        YxArticle yxArticle = yxArticleService.getById(id);
//        BeanUtils.copyProperties(yxArticle, yxArticleDTO);
//        yxArticleService.uploadNews(yxArticleDTO);
//        return new ResponseEntity(HttpStatus.OK);
//    }

}
